8891. Exactly one condition out of two

 

For a given integer n, print “YES” if exactly one of the following conditions is satisfied, and “NO” otherwise:

·        n is an even number;

·        n is a negative number and divisible by three.

 

Input. One single integer n.

 

Output. Print “YES” or “NO” depending on whether the specified conditions are satisfied.

 

Sample input 1

Sample output 1

22

YES

 

 

Sample input 2

Sample output 2

7

NO

 

 

SOLUTION

conditional statement

 

Algorithm analysis

The variable flag will be used to count the number of satisfied conditions. Initially, it is assigned a value of 0.

·        If the number n is even, increment flag by 1;

·        If the number n is negative and divisible by three, also increment flag by 1.

If flag = 1, exactly one condition is satisfied, so print “YES. Otherwise, print NO.

 

Example

Let n = 22. The number is even. It is not negative and divisible by 3. Thus, only one condition is satisfied. The answer is YES.

 

Algorithm implementation

Read the input number n.

 

scanf("%d", &n);

 

Check two conditions. If a condition is satisfied, increment the value of the variable flag by 1.

 

flag = 0;

if (n % 2 == 0) flag++;

if (n < 0 && n % 3 == 0) flag++;

 

Based on the value of flag, print the result.

 

if (flag == 1) puts("YES");

else puts("NO");

 

Python implementation

Read the input number n.

 

n = int(input())

 

Check two conditions. If a condition is satisfied, increment the value of the variable flag by 1.

 

flag = 0

if n % 2 == 0:

  flag += 1

if n < 0 and n % 3 == 0:

  flag += 1

 

Based on the value of flag, print the result.

 

if flag == 1:

  print("YES")

else:

  print("NO")